<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
    :close-on-press-escape="false">
    <el-col :span="24">
      <el-form v-loading="loading" :model="dataForm" :rules="dataRule" ref="dataForm"
        :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
        <el-col :span="12">
          <!-- 填报主题 -->
          <el-form-item label="填报主题" prop="title">
            <!--文本框-->
            <el-input v-model="dataForm.title" placeholder="填报主题" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 周期类型 -->
          <el-form-item label="周期类型" prop="cycleType">
            <el-select v-model="dataForm.cycleType" filterable placeholder="周期类型"
              @change="cycleTypeChangeHandle(dataForm.cycleType)" clearable style="width:100%">
              <el-option v-for="item in cycleTypeList" :key="item.id" :label="item.typeName" :value="item.typeCode">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 开始时间 -->
          <el-form-item label="开始时间" prop="startTime">
            <!--文本框-->
            <el-input v-model="dataForm.startTime" placeholder="开始时间" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 结束时间 -->
          <el-form-item label="结束时间" prop="endTime">
            <!--文本框-->
            <el-input v-model="dataForm.endTime" placeholder="结束时间" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 单位（日，点） -->
          <el-form-item label="单位" prop="unit">
            <!--文本框-->
            <el-input v-model="dataForm.unit" placeholder="单位（日，点）" :disabled="true" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 填报方向 -->
          <el-form-item label="填报方向" prop="unit">
            <el-select v-model="dataForm.typeDirection" placeholder="填报方向" clearable>
                <el-option v-for="template in $getDictList('TYPE_DIRECTION', true)" :key="template.val"
                  :label="template.label" :value="template.val"></el-option>
              </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 路由 -->
          <el-form-item label="路由" prop="url">
            <!--文本框-->
            <el-input v-model="dataForm.url" placeholder="路由" clearable></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 状态 -->
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="dataForm.status">
              <el-radio :label="1">正常</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <!-- 排序 -->
          <el-form-item label="排序" prop="sort">
            <!--计数器-->
            <el-input-number v-model="dataForm.sort" :min="1"></el-input-number>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <!-- 备注说明 -->
          <el-form-item label="备注说明" prop="remark">
            <!--文本域-->
            <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="备注说明"></el-input>
          </el-form-item>
        </el-col>
      </el-form>
    </el-col>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" :loading="loading" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>
<script>
import debounce from 'lodash/debounce'
export default {
  data () {
    return {
      loading: false,
      visible: false,
      cycleTypeList: [],
      dataForm: {
        id: '',
        title: '',
        cycleType: '',
        url: '',
        sort: '',
        remark: '',
        status: 1,
        creator: '',
        createDate: '',
        updater: '',
        updateDate: '',
        startTime: '',
        endTime: '',
        unit: '',
        typeDirection: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        cycleType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        startTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        endTime: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        unit: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        title: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        sort: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.loading = true
          this.getInfo()
        } else {
          this.getSortInfo()
        }
        this.getCycleTypeList()
      })
    },
    // 获取周期类型
    getCycleTypeList () {
      this.$http.get(`/udf/udfCycleType/list`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.cycleTypeList = res.data
      }).catch(() => { })
    },
    // 周期类型改变事件
    cycleTypeChangeHandle (typeCode) {
      let cycleType = this.$getDataItem(this.cycleTypeList, typeCode, 'typeCode')
      this.dataForm.startTime = cycleType.startTime
      this.dataForm.endTime = cycleType.endTime
      this.dataForm.unit = cycleType.unit
      this.dataForm.typeDirection = cycleType.typeDirection
    },
    // 获取排序
    getSortInfo () {
      this.$http.get(`/udf/udfFillingTopic/getMaxSort`).then(({ data: res }) => {
        if (res.code !== 0) {
          return
        }
        this.dataForm.sort = res.data.sort
      }).catch(() => { })
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/udf/udfFillingTopic/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => { })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.loading = true
        this.$http[!this.dataForm.id ? 'post' : 'put']('/udf/udfFillingTopic/', this.dataForm).then(({ data: res }) => {
          this.loading = false
          if (res.code !== 0) {
            return false
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => { })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
